package com.greenzebras.geocache;

import android.graphics.*;
import android.text.*;
import com.google.android.maps.*;

public class TooltipOverlay extends Overlay {

	GeoPoint _point;
	boolean _isVisible = false;
	String _title;
	String _snippet;

	static final float RECT_WIDTH = 220f;
	static final float LINE_HEIGHT = 24f;
	static final float RECT_HEIGHT = LINE_HEIGHT*3;
	static final float RECT_RADIUS = 15f;
	static final float OFFSET = 40f;

	public void setPointAndText(GeoPoint point, String title, String snippet) {
		_point = point;
		_title = title;
		_snippet = snippet;
	}

	public void show() {
		_isVisible = true;
	}

	public void hide() {
		_isVisible = false;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		if (_isVisible) {
			Projection projection = mapView.getProjection();
			Point pt = new Point();
			projection.toPixels(_point, pt);

			// create the balloon path
			Path path = new Path();
			path.moveTo(pt.x, pt.y);
			path.lineTo(pt.x - OFFSET / 2f, pt.y - OFFSET);
			path.lineTo(pt.x + OFFSET / 2f, pt.y - OFFSET);
			path.lineTo(pt.x, pt.y);
			path.addRoundRect(
					new RectF(pt.x - RECT_WIDTH / 2f, pt.y - RECT_HEIGHT - OFFSET, pt.x + RECT_WIDTH / 2f, pt.y - OFFSET),
					RECT_RADIUS, RECT_RADIUS, Path.Direction.CW);

			canvas.save();
			if (shadow) {
				// draw the balloon shadow
				Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(0x50000000);
				paint.setMaskFilter(new BlurMaskFilter(3f, BlurMaskFilter.Blur.NORMAL));
				canvas.skew(-1.0f, 0.0f);
				canvas.scale(1.0f, 0.5f);
				canvas.translate(pt.y, pt.y);
				canvas.drawPath(path, paint);
			}
			else {
				// draw the balloon
				Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(0x9f000000);
				canvas.drawPath(path, paint);

				// draw the title
				paint = new Paint(Paint.ANTI_ALIAS_FLAG);
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(0xffffffff);
				paint.setTextSize(18f);
				paint.setUnderlineText(true);
				canvas.drawText(_title, pt.x - RECT_WIDTH / 2f + RECT_RADIUS, pt.y - OFFSET - RECT_HEIGHT + LINE_HEIGHT, paint);

				// draw the snippet
				paint.setTextSize(14f);
				paint.setUnderlineText(false);
				TextPaint textPaint = new TextPaint(paint);
				StaticLayout layout = new StaticLayout(_snippet, textPaint, (int)(RECT_WIDTH - 2*RECT_RADIUS),
						Layout.Alignment.ALIGN_NORMAL, 1f, 0f, true);
				canvas.translate(pt.x - RECT_WIDTH/2f + RECT_RADIUS,
						pt.y - OFFSET - RECT_HEIGHT + LINE_HEIGHT + 6);
				layout.draw(canvas);
			}
			canvas.restore();
		}
	}
}
